require.config({
    baseUrl:"../",
    paths:{
        jq: "../libs/jquery.2.2.4",
        getJson: "../public/getJson",
        render: "../modules-cart/render",
        setLocal: "../modules-cart/setLocal",
        allNum: "../modules-cart/allNum",
        allPrice: "../modules-cart/allPrice",
        allFlag: "../modules-cart/allFlag",

    }
});

require(["jq","getJson","render","setLocal","allNum","allPrice","allFlag"],function(_, gj, r, sl,allNum,allPrice,allFlag){
    // console.log($)
    console.log(gj)

    let localData=localStorage.getItem("goodsMsg")? JSON.parse(localStorage.getItem("goodsMsg")):[];
    
    $(".allNum").html(allNum(localData));
    $(".allPrice").html(allPrice(localData));
    $("#allFlag")[0].checked=allFlag(localData)

   gj({
       url:"../data/cart.json"
   }).then((res)=>{
    // console.log(JSON.parse(res))
    res = JSON.parse(res)
    r(res,localData,$("tbody"))
   })

   $("tbody").on("input",".setNum",function(){
      let id= $(this).parent().parent().attr("index")
    //    console.log(id,$(this).val())
        sl(localData,id,(i)=>{
            localData[i].num=$(this).val()
        })
        $(".allNum").html(allNum(localData));
        $(".allPrice").html(allPrice(localData));
        nan(this);

   })
   $("tbody").on("click",".del",function(){
    let id= $(this).parent().attr("index")
  //    console.log(id,$(this).val())
        $(this).parent().remove()
        sl(localData,id,(i)=>{
          localData.splice(i,1)
        })

        $(".allNum").html(allNum(localData))
        $(".allPrice").html(allPrice(localData));

    })
    $("tbody").on("input",".check",function(){
        let id= $(this).parent().parent().attr("index")
        //  console.log(id,this.checked)
                
        sl(localData,id,(i)=>{
          localData[i].flag =Number(this.checked)
        })

        $(".allNum").html(allNum(localData))
        $(".allPrice").html(allPrice(localData));
        $("#allFlag")[0].checked=allFlag(localData)

    })
    $("#allFlag").on("input",function(){
        // console.log(this.checked)
        localData.forEach(val => {
            val.flag =Number(this.checked);
        });
        for(var i=0;i<$(".check").length;i++){
            $(".check")[i].checked=this.checked;
        }
        localStorage.setItem ("goodsMsg",JSON.stringify(localData))
    })
    
    function nan(ele){
        var z=ele.parentNode.parentNode.children[5];
        z.innerHTML=ele.value*z.previousElementSibling.innerHTML;

        
    }
});

